<?php
	include_once("global.php");
	session_start();
/*	header("Content-Type:text/html;charset=utf-8");*/
	
	$quit = $_POST['quit'];
	if($quit){
		session_destroy();
		echo "1";
	}

	
//	$love = $_POST['love'];
//	$type = $_POST['type'];
//	if($love && $type){
//		echo "1";
//		$countfile = "./count/".$type.".txt";
//		$fp = fopen($countfile,"rw");
//		$str = fread($fp,filesize($countfile));
//		$str = $str+1;
//		fclose($fp);
//		$handle = fopen($countfile,"w");
//		$love = $str;
//		fwrite($handle,$love);
//		fclose($handle);			
//	}
	/*else if(!$love){
		echo "2";
	}*/
	
//	$like = $_POST['like'];
//	$para = $_POST['para'];
//	if($like&&$para){
//		$sql_select_like = "select A_Applike from appinfo where A_Apptrackid = '$para'";
//		$query_select_like = $mysqli->query($sql_select_like);
//		$result_select_like = $query_select_like->fetch_assoc();
//		$addlike = $result_select_like['A_Applike']+1;
//		$sql_update_like = "update appinfo set A_Applike = '$addlike' where A_Apptrackid = '$para'";
//		$query_update_like = $mysqli->query($sql_update_like);
//		if($query_update_like){
//			echo "1";
//		}else{
//			echo "2";
//		}
//	}

	
	$trackid = $_POST['trackid'];
	$userid = $_POST['uid'];
	/*$trackid = "550392773";
	$userid = "2";*/
	if($trackid && $userid){
		$date = date("Y-m-d");
		$sql_select_user = "select U_Logindate,U_Integral,U_Downloadcount from userinfo where U_UID = '$userid'";
		$query_select_user = $mysqli->query($sql_select_user);
		$result_select_user = $query_select_user->fetch_assoc();
		if($date == $result_select_user['U_Logindate'] && $result_select_user['U_Downloadcount'] < '10'){
			$sql_check_integral = "select P_Pushappid from pushinfo where P_Pushid = '32'";
			$query_check_integral = $mysqli->query($sql_check_integral);
			$result_check_integral = $query_check_integral->fetch_assoc();
			if($result_check_integral['P_Pushappid'] == $trackid){
				$integral = $result_select_user['U_Integral']+5;
				$plus = $result_select_user['U_Downloadcount']+5;
			}else{
				$integral = $result_select_user['U_Integral']+1;
				$plus = $result_select_user['U_Downloadcount']+1;
			}
						
			$sql_update_user = "update userinfo set U_Integral= '$integral',U_Downloadcount = '$plus' where U_UID = '$userid'";
			$query_update_user = $mysqli->query($sql_update_user);
			
			if($query_update_count && $query_update_user){
				echo "1";
			}else{
				echo "2";
			}
		}else{
		
		}
		$sql_select_count = "select A_Appcount from appinfo where A_Apptrackid = '$trackid'";
		$query_select_count = $mysqli->query($sql_select_count);
		$result_select_count = $query_select_count->fetch_assoc();
		$count = $result_select_count['A_Appcount']+1;
		$sql_update_count = "update appinfo set A_Appcount = '$count' where A_Apptrackid = '$trackid'";
		$query_update_count = $mysqli->query($sql_update_count);
	}
	
	$lottery = $_POST['lottery'];
	$id = $_POST['id'];
	if($lottery&& $id){
		$sql_minus_integral = "select U_Integral,U_Consume from userinfo where U_UID = '$id'";
		$query_minus_integral = $mysqli->query($sql_minus_integral);
		$result_minus_integral = $query_minus_integral->fetch_assoc();
		$minus = $result_minus_integral['U_Consume']+25;
		$sql_update_minus = "update userinfo set U_Consume = '$minus' where U_UID = '$id'";
		$query_update_minus = $mysqli->query($sql_update_minus);
		if($query_update_minus){
			$arr_1= array(
				'0' => array('id'=>1,'prize'=>'templates/images/ipad3.png','v'=>0),
				'1' => array('id'=>2,'prize'=>'templates/images/iPhone5.png','v'=>0),
				'2' => array('id'=>3,'prize'=>'templates/images/iPhone4s.png','v'=>0), 
				'3' => array('id'=>4,'prize'=>'templates/images/iPad2.png','v'=>0),
				'4' => array('id'=>5,'prize'=>'templates/images/iTouch5.png','v'=>0),
				'5' => array('id'=>6,'prize'=>'templates/images/iTouch4.png','v'=>0),
				'6' => array('id'=>7,'prize'=>'templates/images/50chongzhika.png','v'=>0),
				'7' => array('id'=>8,'prize'=>'templates/images/10yuanQB.png','v'=>0),
				'8' => array('id'=>9,'prize'=>'templates/images/30jifen.png','v'=>5),
				'9' => array('id'=>10,'prize'=>'templates/images/20jifen.png','v'=>10),
				'10' => array('id'=>11,'prize'=>'templates/images/15jifen.png','v'=>15),
				'11' => array('id'=>12,'prize'=>'templates/images/zaijiezaili.png','v'=>70),
			);	
			foreach($arr_1 as $key => $val) { 
				$arr[$val['id']] = $val['v']; 
			}
			$sum = array_sum($arr);	
			$result = '';
			foreach($arr as $key => $proCur){
				$random = mt_rand(1,$sum);
				if($random > $proCur){
					$sum -= $proCur;
				}else{
					$result = $key;
					break;
				}
			}
			$data = $arr_1[$result-1][prize];		
			echo $data;
			$sql_select_user = "select U_Integral from userinfo where U_UID = '$id'";
			$query_select_user = $mysqli->query($sql_select_user);
			$result_select_user = $query_select_user->fetch_assoc();
			
			if($data == "templates/images/15jifen.png"){
				$integral = $result_select_user['U_Integral']+15;
				$sql_update_user = "update userinfo set U_Integral= '$integral' where U_UID = '$id'";
				$query_update_user = $mysqli->query($sql_update_user);
			}else if($data == "templates/images/20jifen.png"){
				$integral = $result_select_user['U_Integral']+20;
				$sql_update_user = "update userinfo set U_Integral= '$integral' where U_UID = '$id'";
				$query_update_user = $mysqli->query($sql_update_user);
			}else if($data == "templates/images/30jifen.png"){
				$integral = $result_select_user['U_Integral']+30;
				$sql_update_user = "update userinfo set U_Integral= '$integral' where U_UID = '$id'";
				$query_update_user = $mysqli->query($sql_update_user);
			}
			
		}
	}

	$user = $_POST['user'];
	$email = $_POST['email'];
	if($user){
		$sql = "select * from userinfo where U_Loginid = '$user'";
		$query = $mysqli->query($sql);
		$result = $query->fetch_assoc();
		if($result){
			echo "2";
		}else{
			echo "1";
		}
	}
	if($email){
		$sql_check_email = "select * from userinfo where U_email = '$email'";
		$query_check_email = $mysqli->query($sql_check_email);
		$result_check_email = $query_check_email->fetch_assoc();
		if($result_check_email){
			echo "1";
		}else{
			echo "2";
		}
	}
	


?>